<template>
  <div class="login-page">
    <div class="login-container">
      <div class="login-content">
        <!-- 左侧 Logo 和介绍 -->
        <div class="login-banner">
          <div class="banner-content">
            <h1>广告营销管理系统</h1>
            <p class="banner-desc">智能化广告投放 · 数据驱动决策 · 全方位营销管理</p>
          </div>
        </div>

        <!-- 右侧登录表单 -->
        <div class="login-form">
          <div class="form-header">
            <h2>账号登录</h2>
            <p class="welcome-text">欢迎回来！请登录您的账号</p>
          </div>

          <form @submit.prevent="submitForm">
            <div class="form-group">
              <el-input
                v-model="username"
                placeholder="请输入用户名"
                prefix-icon="User"
                :disabled="loading"
              />
            </div>

            <div class="form-group">
              <el-input
                v-model="password"
                type="password"
                placeholder="请输入密码"
                prefix-icon="Lock"
                show-password
                :disabled="loading"
                @keyup.enter="submitForm"
              />
            </div>

            <div class="form-options">
              <el-checkbox v-model="rememberMe">记住我</el-checkbox>
              <!-- <a href="#" class="forgot-password">忘记密码？</a> -->
              <!-- 在登录页的忘记密码链接中 -->
<router-link to="/forgot-password" class="forgot-password">忘记密码？</router-link>
            </div>

            <el-button
              type="primary"
              class="submit-btn"
              :loading="loading"
              @click="submitForm"
            >
              {{ loading ? '登录中...' : '登录' }}
            </el-button>

            <div class="form-footer">
              <span>还没有账号？</span>
              <router-link to="/register" class="register-link">立即注册</router-link>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import { login } from '@/api/user'
import { useUserStore } from '@/stores/user'
import { useWebSocketStore } from '@/stores/websocket'
const router = useRouter()
const username = ref('')
const password = ref('')
const loading = ref(false)
const rememberMe = ref(false)

const submitForm = async () => {
  if (!username.value || !password.value) {
    ElMessage.warning('请输入用户名和密码')
    return
  }

  try {
    loading.value = true
    const { data } = await login({
      username: username.value,
      password: password.value
    })

    console.log('登录响应数据:', data)
    if (data.code === 200) {
      const userStore = useUserStore()
      userStore.setToken(data.data.token)
      const userInfo = await userStore.getUserInfo()
      userStore.setUserInfo(userInfo)
      ElMessage.success('登录成功')
      const webSocketStore = useWebSocketStore();
      webSocketStore.initWebSocket(userStore.token);
      // 根据角色类型跳转
      switch (userInfo.roleType) {
        case 1:
          router.push('/admin/users')
          break
        case 2:
          router.push('/advertiser/dashboard')
          break
        case 3:
          router.push('/device-owner/dashboard')
          break
        default:
          ElMessage.warning('未知角色类型，请联系管理员')
          router.push('/userinfo')
      }
    } else {
      ElMessage.error(data.message || '登录失败')
    }
  } catch (error) {
    console.error('登录失败:', error)
    ElMessage.error('登录请求失败，请检查网络')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #1890ff 0%, #1d39c4 100%);
  padding: 20px;
}

.login-container {
  width: 1000px;
  max-width: 90%;
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.login-content {
  display: flex;
  min-height: 600px;
}

.login-banner {
  flex: 1;
  background: linear-gradient(135deg, #1890ff 0%, #1d39c4 100%);
  padding: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  position: relative;
  overflow: hidden;
}

.banner-content {
  position: relative;
  z-index: 1;
  text-align: center;
}

.banner-content h1 {
  font-size: 2.5em;
  margin-bottom: 20px;
  font-weight: 600;
}

.banner-desc {
  font-size: 1.1em;
  opacity: 0.9;
  line-height: 1.6;
}

.login-form {
  flex: 1;
  padding: 40px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.form-header {
  text-align: center;
  margin-bottom: 40px;
}

.form-header h2 {
  font-size: 28px;
  color: #333;
  margin-bottom: 10px;
}

.welcome-text {
  color: #666;
  font-size: 16px;
}

.form-group {
  margin-bottom: 24px;
}

.form-group :deep(.el-input__wrapper) {
  padding: 12px;
  box-shadow: 0 0 0 1px #dcdfe6;
}

.form-group :deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px #1890ff;
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.forgot-password {
  color: #1890ff;
  text-decoration: none;
  font-size: 14px;
}

.forgot-password:hover {
  color: #40a9ff;
}

.submit-btn {
  width: 100%;
  padding: 12px;
  font-size: 16px;
  margin-bottom: 24px;
}

.form-footer {
  text-align: center;
  color: #666;
  font-size: 14px;
}

.register-link {
  color: #1890ff;
  text-decoration: none;
  margin-left: 8px;
}

.register-link:hover {
  color: #40a9ff;
}

@media (max-width: 768px) {
  .login-content {
    flex-direction: column;
  }

  .login-banner {
    padding: 30px;
    min-height: 200px;
  }

  .banner-content h1 {
    font-size: 2em;
  }

  .login-form {
    padding: 30px;
  }
}
</style>